package com.dsg.controller.oil;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.oil.DsgOilStation;
import com.dsg.service.oil.IDsgOilStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 油站设置 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-27
 */
@RestController
@RequestMapping("/oil/dsgOilStation")
@Api(description = "油站设置")
public class DsgOilStationController extends BaseController {

    @Autowired
    private IDsgOilStationService dsgOilStationService;

    /**
     * 查询列表（分页）
     *
     * @param dsgOilStation
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgOilStation dsgOilStation) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        if (StringUtil.isEmpty(sort)) {
            orderItemList.add(new OrderItem().setColumn("CREATE_TIME").setAsc(false));
            page.setOrders(orderItemList);
        }
        dsgOilStation.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = dsgOilStationService.selectByList(page, dsgOilStation);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 查询单条数据
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}/view")
    @ApiOperation("查询单条数据")
    public AjaxResult view(@PathVariable String id) {
        DsgOilStation dsgOilStation = dsgOilStationService.getById(id);
        return json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilStation);
    }

    /**
     * 新增
     *
     * @param dsgOilStation
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgOilStation dsgOilStation) {
        dsgOilStation.setOid(UUIDGenerator.getUUID());
        boolean flag = dsgOilStationService.save(dsgOilStation);
        if (flag) {
            return json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilStation);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 更新
     *
     * @param dsgOilStation
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgOilStation dsgOilStation) {
        AjaxResult ajaxResult;
        boolean flag = dsgOilStationService.updateById(dsgOilStation);
        if (flag) {
            ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilStation);
        } else {
            ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("批量删除")
    public AjaxResult delete(@ApiParam(value = "id以，号分隔", name = "idList", required = true) @RequestParam String idList) {
        Boolean flag = dsgOilStationService.removeByIds(Arrays.asList(idList.split(",")));
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }
}
